using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using csammisrun.shaim;

namespace DefaultUI
{
  /// <summary>
  /// Interaction logic for SystemPage.xaml
  /// </summary>

  internal partial class SystemPage : System.Windows.Controls.Page
  {
    private ICore _core = null;

    public SystemPage()
    {
      InitializeComponent();
      _core = DefaultUISet.Core;

      LoadProxySettings();
    }

    private void LoadProxySettings()
    {
      ShaimProxyInfo info = _core.GlobalProxySetting;
      UseGlobalProxy.IsChecked = (info.ProxyType != ShaimProxyType.None);

      ProxyTypeList.Items.Clear();
      foreach (ShaimProxyType type in Enum.GetValues(typeof(ShaimProxyType)))
      {
        if (type == ShaimProxyType.None)
        {
          continue;
        }
        ProxyTypeList.Items.Add(type);
      }
      if (info.ProxyType != ShaimProxyType.None)
      {
        ProxyTypeList.SelectedItem = info.ProxyType;
      }
      else
      {
        ProxyTypeList.SelectedIndex = 0;
      }

      Server.Text = info.Server;
      Port.Text = info.Port.ToString();
      Username.Text = info.Username;
      Password.Password = info.Password;
      ResetButton.IsEnabled = false;
      ApplyButton.IsEnabled = false;
    }

    private void ApplyButton_Click(object sender, RoutedEventArgs e)
    {
      int port = -1;

      if (Server.Text.Trim() == "")
      {
        MessageBox.Show("The Server field must be filled in", "Invalid server setting");
        Server.Focus();
        return;
      }

      if (Int32.TryParse(Port.Text, out port) == false || !(port > 0 && port <= ushort.MaxValue))
      {
        MessageBox.Show("The Port value must be a number between 1 and " + ushort.MaxValue.ToString(), "Invalid port setting");
        Port.SelectAll();
        Port.Focus();
        return;
      }

      _core.SetProxyInfo((ShaimProxyType)ProxyTypeList.SelectedItem, Server.Text, port, Username.Text, Password.Password);
      LoadProxySettings();
    }

    private void ResetButton_Click(object sender, RoutedEventArgs e)
    {
      LoadProxySettings();
    }

    private void ProxyTypeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      ApplyButton.IsEnabled = ResetButton.IsEnabled = true;
    }

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
      ApplyButton.IsEnabled = ResetButton.IsEnabled = true;
    }

    private void UseGlobalProxy_Checked(object sender, RoutedEventArgs e)
    {
      ProxySettings.IsEnabled = UseGlobalProxy.IsChecked.Value;

      if (UseGlobalProxy.IsChecked.Value == false)
      {
        int port = 0;
        Int32.TryParse(Port.Text, out port);
        _core.SetProxyInfo(ShaimProxyType.None, Server.Text, port, Username.Text, Password.Password);
      }
    }
  }
}